/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.page;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author HY
 */
public class SimpleKurvyBook implements Book
{
    BookTemplate template;
    Vector<PageSpread> spreads;

    public SimpleKurvyBook( BookTemplate bookTemplate )
    {
        this.template = bookTemplate;
    }

    public BookTemplate getBookTemplate()
    {
        return this.template;
    }

    public PageSpread[] getPageSpreadArray()
    {
        if( spreads != null && !spreads.isEmpty() )
        {
            PageSpread[] spreadArray = new PageSpread[ spreads.size() ];
            int i = 0;
            for( Iterator<PageSpread> it = spreads.iterator(); it.hasNext(); i++ )
            {
                spreadArray[ i ] = it.next();
            }
            return spreadArray;
        }
        else
        {
            return new PageSpread[ 0 ];
        }
    }

    public ArrayList<PageSpread> getPageSpreads()
    {
        ArrayList<PageSpread> result = new ArrayList<PageSpread>();
        if( spreads != null && !spreads.isEmpty() )
        {
            result.addAll( spreads );
        }
        return result;
    }

    public void addPageSpread( PageSpread spread )
    {
        if( spreads == null )
            spreads = new Vector<PageSpread>();

        spreads.add( spread );
    }

    public void addPageSpread( PageSpread spread, int index )
    {
        if( spreads == null )
            spreads = new Vector<PageSpread>();

        spreads.add( index, spread );
    }

    public void addPageSpreadBefore( PageSpread spread, PageSpread before )
    {
        if( spreads == null )
            spreads = new Vector<PageSpread>();

        int index = 0;
        if( before != null )
            index = spreads.indexOf( before );

        spreads.add( ( index >= 0 )? index : 0 , spread );

    }

    public void addPageSpreadAfter( PageSpread spread, PageSpread after )
    {
        if( spreads == null )
            spreads = new Vector<PageSpread>();

        int index = 0;
        if( after != null )
            index = spreads.indexOf(  after ) + 1;

        spreads.add( (index >= 0 )? index : 0, spread );
    }

    public void removePageSpread( PageSpread spread )
    {
        if( spreads != null )
        {
            spreads.remove( spread );
        }
    }

    public void removePageSpread( ArrayList<PageSpread> spreads )
    {
        if( spreads != null )
        {
            spreads.removeAll(  spreads );
        }
    }

    public void removePageSpread( int from, int to )
    {
        if( spreads != null )
        {
            for( int i = from; i <= to; i++ )
            {
                spreads.remove(  i );
            }
        }
    }

    public void clearPages()
    {
        if( spreads != null )
        {
            spreads.clear();
        }
    }

    public PageSpread getFirstPageSpread()
    {
        if( spreads != null && !spreads.isEmpty() )
            return spreads.firstElement();
        else
            return null;
    }

    public PageSpread getNextPageSpread( PageSpread spread )
    {
        if( spreads != null && !spreads.isEmpty() )
        {
            int index = spreads.indexOf( spread );
            if( index + 1 < spreads.size() )
            {
                return spreads.elementAt( index + 1 );
            }
        }
        return null;
    }

    public PageSpread getPreviousPageSpread( PageSpread spread )
    {
        if( spreads != null && !spreads.isEmpty() )
        {
            int index = spreads.indexOf( spread );
            if( index - 1 >= 0 )
            {
                return spreads.elementAt( index - 1 );
            }
        }
        return null;
    }

    public PageSpread getPageSpread( int page )
    {
        if( spreads != null && !spreads.isEmpty() )
        {
            if( page % 2 < spreads.size() )
            {
                return spreads.elementAt( page % 2 );
            }
        }
        return null;
    }

    public PageSpread createNewPageSpread()
    {
        PageSpread spread = new PageSpread();
        spread.setPageSpreadMask( this.template.getPageSpreadMask() );
        spread.setPageLayoutTemplate( this.template.getLeftPageTemplate() );
        spread.setPageLayoutTemplate( this.template.getRightPageTemplate() );
        return spread;
    }

    public PageSpread createNewPageSpread( PageLayoutTemplate left, PageLayoutTemplate right )
    {
        PageSpread spread = new PageSpread();
        spread.setPageSpreadMask( this.template.getPageSpreadMask() );
        spread.setPageLayoutTemplate( left );
        spread.setPageLayoutTemplate( right );
        return spread;
    }
}
